package com.mk.common.office;

import org.springframework.beans.factory.annotation.Value;

/**
 * @Author: yq
 * @Description:  用pdf2htmlEX 工具将pdf转换为html
 * @Date: Created in 3:56 PM 3/19/18
 */
public class Pdf2htmlEXUtil {


    @Value("${pdf2htmlex.path}")
    private static String pdf2htmlEXPath;

    /**
     * 调用 pdf2htmlEX 见pdf文件转换为html
     *
     * @param pdfName 需要转换的pdf文件路径+名称
     * @param htmlName  生成的html文件路径+名称
     * @return
     */
    public static boolean pdf2html(String pdfName,String htmlName){
        Runtime rt = Runtime.getRuntime();

        try{
            Process p = rt.exec(pdf2htmlEXPath + " " + pdfName + " " + htmlName);
            StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(),"ERROR");
            //kick off stderr
            errorGobbler.start();
            StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(),"STDOUT");
            //Kick off stdout
            outGobbler.start();
            p.waitFor();
//            int w = p.waitFor();
//            System.out.println(w);
            p.exitValue();
//            int v = p.exitValue();
//            System.out.println(v);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(pdf2html("F:\\office\\1.pdf","F:\\office\\pdf\\1.html"));
    }
}
